<?php
/**
 * 
 */
class Index  extends Controller
{
	
	public function index()
	{
		$db = new Db('msg');

		$total = $db->count();
		// 总数

		// 每页多少条
		$num = 2;
		//  一共多少页
		$pages = ceil($total/$num);
		// 当前在第几页
		$p = isset($_GET['p']) ? $_GET['p'] : 1;


		$sql = "select t1.*,t2.username from msg t1 inner join user t2 on t1.uid=t2.user_id order by msg_id desc limit ".($p-1)*$num.",".$num;

		$oldData = $db->query($sql);

		// 计算开始值与结束值 
		if($p<3){
			$start = 1;
			$end = 5;
		}elseif($p>($pages-2)){
			$start = $pages-4;
			$end = $pages;
		}else{
			$start = $p-2;
			$end = $p+2;
		}

		// 分配变量
		$this->assign('oldData',$oldData);
		$this->assign('start',$start);
		$this->assign('end',$end);
		$this->assign('p',$p);
		$this->assign('pages',$pages);
		// 引视图
		$this->display();
	}
}